package com.example.util;

import com.alibaba.fastjson.JSONObject;
import com.google.zxing.BarcodeFormat;
import com.google.zxing.EncodeHintType;
import com.google.zxing.MultiFormatWriter;
import com.google.zxing.client.j2se.MatrixToImageWriter;
import com.google.zxing.common.BitMatrix;

import java.nio.file.FileSystems;
import java.nio.file.Path;
import java.util.HashMap;
import java.util.Map;

public class QrCodeUtil_bilibili {

    /**
     * 二维码颜色
     */
    private static final int BLACK = 0xFF000000;
    private static final int WHITE = 0xFFFFFFFF;
    /**
     * LOGO宽度
     */
    private static final int LOGO_WIDTH = 60;
    /**
     * LOGO高度
     */
    private static final int LOGO_HEIGHT = 60;
    /**
     * 图片的宽度
     */
    private static int WIDTH = 300;
    /**
     * 图片的高度
     */
    private static int HEIGHT = 300;
    /**
     * 图片的格式
     */
    private static String FORMAT = "png";

    public static void main(String[] args) {
        try {
            JSONObject jsonObject = new JSONObject();
            jsonObject.put("name", "张国庆");
            jsonObject.put("gender", "男");
            jsonObject.put("address", "黄河滩");
//            jsonObject.put("company", "http://www.baidu.com");
//            jsonObject.put("ad", "asdfghjkl;xcvbnMsdedrftghjkl;xcvbnm,");
            String content = jsonObject.toString();
            content = "http://114.116.39.18:8900/loginController/patchca";
            Map<EncodeHintType, Object> map = new HashMap<>();
            map.put(EncodeHintType.CHARACTER_SET, "UTF-8");
            // 创建一个矩阵对象
            BitMatrix bitMatrix = new MultiFormatWriter().encode(content, BarcodeFormat.QR_CODE, WIDTH, HEIGHT, map);
            // 将矩阵对象生成一个图片
            String filePath = "G:\\";
            String fileName = "bilibili1_patchca.jpg";
            Path path = FileSystems.getDefault().getPath(filePath, fileName);
            MatrixToImageWriter.writeToPath(bitMatrix, "jpg", path);
            System.out.println("生成二维码成功");
        } catch (Exception ex) {
            ex.printStackTrace();
        }
    }


}
